package com.shiyi.shiyiaicodefather.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.img.ImgUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.shiyi.shiyiaicodefather.exception.BusinessException;
import com.shiyi.shiyiaicodefather.exception.ErrorCode;
import com.shiyi.shiyiaicodefather.exception.ThrowUtils;
import io.github.bonigarcia.wdm.WebDriverManager;
import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.time.Duration;

/**
 *  网页截图工具类
 */
@Slf4j
public class WebScreenshotUtils {

  private static final WebDriver webDriver;

  static {
    final int DEFAULT_WIDTH = 1600;
    final int DEFAULT_HEIGHT = 900;
    webDriver = initChromeDriver(DEFAULT_WIDTH,DEFAULT_HEIGHT);
  }

  /**
   * 生成网页截图
   *
   * @param webUrl 网页URL
   * @return 压缩后的截图文件路径，失败返回null
   */
  public static String saveWebPageScreenshot(String webUrl) {
    if (StrUtil.isBlank(webUrl)) {
      log.error("网页url不能为空");
      return null;
    }
    //创建临时目录
    String compressImageSavePath = null;
    try {
      String rootPath = System.getProperty("user.dir") + File.separator + "tmp" + File.separator + "web_screenshot" + File.separator + UUID.randomUUID().toString().substring(0,8);
      FileUtil.mkdir(rootPath);
      //图片后缀
      final String IMAGE_SUFFIX = ".png";
      //访问网页
      webDriver.get(webUrl);
      //等待页面加载完成
      waitForPageLoad(webDriver);
      //截图
      byte[] screenshotAs = ((TakesScreenshot) webDriver).getScreenshotAs(OutputType.BYTES);
      //原始图片路径
      String imageSavePath = rootPath + File.separator + UUID.randomUUID().toString().substring(0,5) + IMAGE_SUFFIX;
      //保存原始图片
      saveImage(screenshotAs,imageSavePath);
      //压缩文件
      //图片路径
      final String COMPRESSION_SUFFIX = "_compress.jpg";
      compressImageSavePath = rootPath + File.separator + UUID.randomUUID().toString().substring(0,5) + COMPRESSION_SUFFIX;
      compressImage(imageSavePath,compressImageSavePath);
      log.info("压缩图片保存成功{}",compressImageSavePath);
      //删除原始文件只保留压缩文件
      FileUtil.del(imageSavePath);
      return compressImageSavePath;
    } catch (WebDriverException e) {
      log.error("网页截图失败: {}", webUrl, e);
      return null;
    }
  }
  /**
   * 保存图片到文件
   */
  private static void saveImage(byte[] imageBytes, String imagePath) {
    try {
      FileUtil.writeBytes(imageBytes,imagePath);
    } catch (IORuntimeException e) {
      log.error("保存图片失败: {}", imagePath, e);
      throw new BusinessException(ErrorCode.SYSTEM_ERROR, "保存图片失败");
    }
  }

  /**
   * 压缩图片
   */
  private static void compressImage(String originalImagePath, String compressedImagePath) {
    // 压缩图片质量（0.1 = 10% 质量）
    final float COMPRESSION_QUALITY = 0.3f;
    try {
      ImgUtil.compress(FileUtil.file(originalImagePath), FileUtil.file(compressedImagePath), COMPRESSION_QUALITY);
    } catch (IORuntimeException e) {
      log.error("压缩图片失败: {}", originalImagePath, e);
      throw new BusinessException(ErrorCode.SYSTEM_ERROR, "压缩图片失败");
    }
  }


  /**
   * 等待页面加载完成
   */
  private static void waitForPageLoad(WebDriver driver) {
    try {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    wait
      .until(webDriver -> ((JavascriptExecutor) webDriver)
        .executeScript("return document.readyState").equals("complete"));
      //额外等待一段时间确保动态内容加载完成
      Thread.sleep(2000);
      log.info("页面加载完成");
      } catch (InterruptedException e) {
        log.error("等待页面加载时出现异常，继续执行截图", e);
      }
  }

  private static WebDriver initChromeDriver(int width, int height) {
    try {
      // 自动管理 ChromeDriver
      WebDriverManager.chromedriver().setup();
      // 配置 Chrome 选项
      ChromeOptions options = new ChromeOptions();
      // 无头模式
      options.addArguments("--headless");
      // 禁用GPU（在某些环境下避免问题）
      options.addArguments("--disable-gpu");
      // 禁用沙盒模式（Docker环境需要）
      options.addArguments("--no-sandbox");
      // 禁用开发者shm使用
      options.addArguments("--disable-dev-shm-usage");
      // 设置窗口大小
      options.addArguments(String.format("--window-size=%d,%d", width, height));
      // 禁用扩展
      options.addArguments("--disable-extensions");
      // 设置用户代理
      options.addArguments("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
      // 创建驱动
      WebDriver driver = new ChromeDriver(options);
      // 设置页面加载超时
      driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));
      // 设置隐式等待
      driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
      return driver;
    } catch (Exception e) {
      log.error("初始化 Chrome 浏览器失败", e);
      throw new BusinessException(ErrorCode.SYSTEM_ERROR, "初始化 Chrome 浏览器失败");
    }
  }



}
